package com.itheima.controller;


import com.itheima.dao.DirDao;
import com.itheima.domain.Dir;
import com.itheima.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dir")
public class DirHandler {


    final
    DirDao dirDao;

    public DirHandler(DirDao dirDao) {
        this.dirDao = dirDao;
    }

    @ResponseBody
    @RequestMapping("offShare")
    public String offShare(Integer did){
        dirDao.offShare(did);
        return "ok";
    }
    @ResponseBody
    @RequestMapping("onShare")
    public String onShare(Integer did){
        dirDao.onShare(did);
        return "ok";
    }
    @ResponseBody
    @RequestMapping("delDir")
    public String delDir(Integer did){
        System.out.println(did);
        dirDao.delDir(did);
        return "ok";
    }


    @ResponseBody
    @RequestMapping("/findAllDir")
    public List<Dir> findAllDir(HttpSession session){
        User u= (User) session.getAttribute("User");
        System.out.println(u);
        return dirDao.findAllByUid(u.getId()) ;
    }
    @ResponseBody
    @RequestMapping("/findAllImage")
    public List<Dir> findAllImage(HttpSession session){
        User u= (User) session.getAttribute("User");
        System.out.println(u);
        List<Dir> dirs=dirDao.findAllByUid(u.getId());
        List<Dir> dirs1=new ArrayList<Dir>();
        for (Dir d:dirs){
            if (d.getDname().endsWith("png")||d.getDname().endsWith("jpg")){
                dirs1.add(d);
            }
        }
        return dirs1;
    }
    @ResponseBody
    @RequestMapping("/findAllVideo")
    public List<Dir> findAllVedio(HttpSession session){
        User u= (User) session.getAttribute("User");
        System.out.println(u);
        List<Dir> dirs=dirDao.findAllByUid(u.getId());
        List<Dir> dirs1=new ArrayList<Dir>();
        for (Dir d:dirs){
            if (d.getDname().endsWith("mp4")){
                dirs1.add(d);
            }
        }
        return dirs1;
    }
    @ResponseBody
    @RequestMapping("/findAllText")
    public List<Dir> findAllText(HttpSession session){

        System.out.println("Text");
        User u= (User) session.getAttribute("User");
        System.out.println(u);
        List<Dir> dirs=dirDao.findAllByUid(u.getId());
        List<Dir> dirs1=new ArrayList<Dir>();
        for (Dir d:dirs){
            if ((d.getDname().endsWith("txt")||d.getDname().endsWith("doc")||d.getDname().endsWith("docx")||d.getDname().endsWith("xls")||d.getDname().endsWith("xlsx"))){
                dirs1.add(d);
            }
        }
        return dirs1;
    }
    @ResponseBody
    @RequestMapping("/findAllOther")
    public List<Dir> findAllOther(HttpSession session){
        System.out.println("Other");
        User u= (User) session.getAttribute("User");
        System.out.println(u);
        List<Dir> dirs=dirDao.findAllByUid(u.getId());
        List<Dir> dirs1=new ArrayList<Dir>();
        for (Dir d:dirs){
            if (!(d.getDname().endsWith("jpg")||d.getDname().endsWith("png")||d.getDname().endsWith("mp4")||d.getDname().endsWith("txt")||d.getDname().endsWith("doc")||d.getDname().endsWith("docx")||d.getDname().endsWith("xls")||d.getDname().endsWith("xlsx"))){
                dirs1.add(d);
            }
        }
        return dirs1;
    }

    @ResponseBody
    @RequestMapping("/findAllShare")
    public List<Dir> findAllShare(){
        return dirDao.findAllShare();
    }

    @ResponseBody
    @RequestMapping("/findShare")
    public List<Dir> findShare(HttpSession session){
        User u =(User) session.getAttribute("User");
        return dirDao.findShare(u.getId());
    }




    @ResponseBody
    @RequestMapping("/upload")
    public String upload(String username, MultipartFile file, HttpSession session, HttpServletResponse response) throws IOException {
        if (file==null){
            return "no";
        }
        String path ="";
        // 获取static目录 获取static就可以完成上传功能了
        String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
        System.out.println(path);
        path=realPath+"/"+username;
        System.out.println(path);
        File pathFile=new File(path);
        if (!pathFile.exists()){
            System.out.println("no");
            pathFile.mkdirs();
        }else {
            System.out.println("yes");
        }
        String newname= String.valueOf(System.currentTimeMillis());
        System.out.println(file.getOriginalFilename());
//        String[] sufix=(file.getOriginalFilename()).split("[.]");
//        String newname2=newname+'.'+sufix[1];
        String newname3=file.getOriginalFilename();
        File file2=new File(path,newname3);
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ddate=sd.format(new Date());
        System.out.println(ddate);
        System.out.println("file upload ok~");
        dirDao.AddDir(1,ddate,newname3,"is", (double) file.getSize(),path);



        //

        File filez = null;
        try {

            String[] filename = file.getOriginalFilename().split("[.]");
            System.out.println(file.getOriginalFilename());
            filez=File.createTempFile(filename[0], filename[1]);
            file.transferTo(filez);
            filez.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //
        FileInputStream fis=new FileInputStream(filez);
        FileOutputStream fos=new FileOutputStream(file2);
        BufferedInputStream bis=new BufferedInputStream(fis);
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        byte[] bys=new byte[102];
        int len=0;
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        if (bos!=null){
            bos.close();
        }
        if (bis!=null){
            bis.close();
        }

//        file.transferTo(file2);
        return  null;
    }





}
